﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Npgsql;

namespace SIGEB.Modelo.moduloTipoDocumentos
{
    public class conjuntoTipoDocumentos:conjuntoBase
    {
        #region "constructoes"

        public conjuntoTipoDocumentos(datosAplicacion dA)
            : base(dA)
        {
            numTipo = CONJTIPODOCS;
        }

        #endregion

        #region "metodos"

        public override object obtenerEstado()
        {
            throw new NotImplementedException();
        }

        #region "imec"

        public void agregartipoDocumento(tipoDocumento nuevotipoDocumento)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT registrar_tipo_documento('" + nuevotipoDocumento.nombre + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al agregar tipoDocumento: " + nuevotipoDocumento.codTipoDocumento);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public void eliminartipoDocumento(String codtipoDocumento)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT eliminar_tipo_documento('" + codtipoDocumento + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al eliminar tipoDocumento: " + codtipoDocumento);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public void modificartipoDocumento(String codtipoDocumento, tipoDocumento nuevotipoDocumento)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT actualizar_tipo_documento('" + codtipoDocumento + "', '" + nuevotipoDocumento.nombre + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al editar tipoDocumento: " + codtipoDocumento);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public tipoDocumento obtenertipoDocumento(String codtipoDocumento)
        {
            tipoDocumento nueva = null;
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_tipo_documento_id('" + codtipoDocumento + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];

            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el tipoDocumento " + codtipoDocumento);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            string[] valores;
            foreach (DataRow row in dt.Rows)
            {
                try
                {
                    valores = datosDataSet(row, 2);
                    nueva = new tipoDocumento(valores[0], valores[1], datos.atributos.obteneratributosIdDTipoDoc(valores[0]));
                }
                catch { }
            }
            return nueva;
        }

        public tipoDocumento obtenertipoDocumentoNombre(String NombretipoDocumento)
        {
            tipoDocumento nueva = null;
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_tipo_documento_nombre('" + NombretipoDocumento + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 2);
                    nueva = new tipoDocumento(valores[0], valores[1], datos.atributos.obteneratributosIdDTipoDoc(valores[0]));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el tipoDocumento " + NombretipoDocumento);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            return nueva;
        }

        public LinkedList<tipoDocumento> obtenerTiposDocumento()
        {
            LinkedList<tipoDocumento> nueva = new LinkedList<tipoDocumento>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_tipo_documento()";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener los tipos de documento");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            string[] valores;
            foreach (DataRow row in dt.Rows)
            {
                valores = datosDataSet(row, 2);
                nueva.AddLast(new tipoDocumento(valores[0], valores[1], datos.atributos.obteneratributosIdDTipoDoc(valores[0])));
            }
            return nueva;
        }

        public tipoDocumento tipoDocPorIdDocumento(string idDocumento)
        {
            tipoDocumento nueva = null;
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_tipo_documento_id('" + idDocumento + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 2);
                    nueva = new tipoDocumento(valores[0], valores[1], null);    
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el tipoDocumento por IdDoc " + idDocumento);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            if (nueva != null)
            {
                nueva.atributos = datos.atributos.obteneratributosIdDTipoDoc(nueva.codTipoDocumento);
            }
            throw new NotFiniteNumberException();
            //return nueva;
        }
        #endregion

        #endregion
    }

}
